package com.fly.caihong.frag;

import android.os.Looper;
import android.os.MessageQueue;
import android.util.Pair;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fly.caihong.R;
import com.fly.caihong.adapter.MessageAdapter;
import com.fly.caihong.base.BasePageListFragment;
import com.fly.caihong.model.MessageList;
import com.fly.caihong.utils.DataHandler;
import com.fly.caihong.utils.ToastUtils;
import com.fly.caihong.view.PullRefreshRecyclerView;

import java.util.ArrayList;

public class MessageFragment extends BasePageListFragment {

    @Override
    protected Pair<PullRefreshRecyclerView, BaseQuickAdapter> initPullRefreshView(View view) {
        return new Pair(view.findViewById(R.id.pull_refresh_layout), new MessageAdapter(new ArrayList()));
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        dismissRefreshStatus();
        MessageList messageItems = DataHandler.convertJsonData("message", MessageList.class);
        if (messageItems == null) {
            ToastUtils.showLong("消息数据为空");
            return;
        }
        mAdapter.addData(messageItems.getMessageList());

    }

    @Override
    protected void loadPageData(int pageNum) {
        dismissRefreshStatus();
    }

    @Override
    public void onRefresh() {
        dismissRefreshStatus();
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_message;
    }

    @Override
    public void initData() {
        super.initData();
        Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
            @Override
            public boolean queueIdle() {
                mAdapter.loadMoreComplete();
                mAdapter.loadMoreEnd(true);
                return false;
            }
        });
    }
}
